<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Callplatform extends CrmController
{
    /**
     * 渲染模板outBoundList
     */
    public function outBoundList()
    {
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);

        $this->assign("task_id",$_REQUEST['id']);
        // 模板输出
        return $this->fetch('outBoundList');
    }


    public function saleSourceData()
    {
        $where = "createuser = '".$_SESSION['user_info']['username']."'";
        // 模板输出
        $arrData = Loader::model('Callplatform')->saleSourceData($where);
        echo json_encode($arrData);
    }


    public function myCustomer ()
    {
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        
        $this->assign("task_id",$_REQUEST['task_id']);
        $this->assign("extension",$_SESSION['user_info']['extension']);
        // 模板输出
        return $this->fetch('myCustomer');
    }


    public function taskList ()
    {
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];
        $this->assign("priv",$priv);

        // 模板输出
        return $this->fetch('taskList');
    }



    public function taskListData()
    {
        $arrData = Loader::model('Callplatform')->taskList();
        echo json_encode($arrData);
    }


    /**
     * 只有自动外呼弹屏调用!
     */
    public function editCustomer ()
    {
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // print_r($_REQUEST);exit;
        $phone = $_REQUEST['phone'];
        $id = $_REQUEST['id'];
        $customerData = Loader::model('Callplatform')->getCustomerData($phone, $id);
        if (empty($customerData)) {
            echo "当前外呼任务中不存在此客户，请前往客户资料查看！";
            exit;
        }
        $this->assign("customerData",$customerData); // 客户信息
        $this->assign("id",$id); // 任务id
        $illnessSelect = Cache::get('illnessSelect');
        $illness_option = '';
        foreach ($illnessSelect as $val) {
            $illness_option .= '<option value="'.$val['id'].'">'.$val['illness_type'].'</option>';
        }
        $this->assign('illness_option', $illness_option); // 病情类型
        $times = date("Y-m-d H:i:s");
        $this->assign("times",$times); // 当前时间
        // 模板输出
        return $this->fetch('callplatform/editCustomer');
    }


    public function editMyCustomer ()
    {
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        // print_r($_REQUEST);exit;
        $customer_code = $_REQUEST['customer_code'];
        $id = $_REQUEST['id'];
        $customerData = Loader::model('Callplatform')->getMyCustomerData($customer_code, $id);
        if (empty($customerData)) {
            echo "当前外呼任务中不存在此客户，请前往客户资料查看！";
            exit;
        }
        $this->assign("customerData",$customerData); // 客户信息
        $this->assign("id",$id); // 任务id
        $illnessSelect = Cache::get('illnessSelect');
        $illness_option = '';
        foreach ($illnessSelect as $val) {
            $illness_option .= '<option value="'.$val['id'].'">'.$val['illness_type'].'</option>';
        }
        $this->assign('illness_option', $illness_option); // 病情类型
        $times = date("Y-m-d H:i:s");
        $this->assign("times",$times); // 当前时间
        // 模板输出
        return $this->fetch('callplatform/editCustomer');
    }


    public function updateCustomer()
    {
        $data = $_REQUEST;
        $location_address = explode('/', $data['location_address']);
        $customerData['province'] = $location_address[0];
        $customerData['city'] = $location_address[1];
        $customerData['district'] = $location_address[2];
        
        $customerData['address'] = $data['address_details'];
        $customerData['name'] = $data['name'];
        $customerData['createuser'] = $_SESSION['user_info']['username'];
        $customerData['sex'] = $data['sex'];
        $customerData['age'] = $data['age'];
        $customerData['customer_code'] = $data['customer_code'];
        // print_r($data);exit;
        $result = Loader::model('Callplatform')->updateCustomer($customerData, $data['id']);

        if ($result) {
            echo json_encode(array('success'=>true,'msg'=>'保存成功！'));
        } else {
            echo json_encode(array('msg'=>'保存失败！'));
        }
    }


    /**
     * 获取客户资料列表
     * @return [json] [客户资料列表]
     */
    public function customerDataList()
    {
        // $users = Cache::get('users');
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        // print_r($_REQUEST);exit;
        //需先判断传值是否为空
        $where = "(1 ";
        $where .= " AND recovery = 1";
        $where .= " AND create_user = '".$_SESSION['user_info']['username']."'";
        $where .= empty($_REQUEST["name"])?"":" AND name like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone like '%".$_REQUEST["phone"]."%'";
        $where .= empty($_REQUEST["import_mark"])?"":" AND import_mark like '%".$_REQUEST["import_mark"]."%'";
        $where .= empty($_REQUEST["sex"])?"":" AND sex = '".$_REQUEST["sex"]."'";

        $where .= " )";
    
        $arrData = Loader::model('Servicecustomer')->getCustomerData($where,$page,$limit);
        echo json_encode($arrData);
    }


    public function outBoundRecord()
    {
        //分配增删改的权限
        $menuname = "Call Platform";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname]; 
        $this->assign("priv",$priv);
        // print_r($_REQUEST['id']);exit;
        $this->assign("id",$_REQUEST['task_id']);
        // 模板输出
        return $this->fetch('outBoundRecord');
    }



    public function taskRecordsList()
    {
        $id = $_REQUEST['id'];
        $exten = $_SESSION['user_info']['extension'];
        if (empty($exten)) {
            exit;
        }
        $where = "(1 ";
        $where .= " AND dst = $exten";
        $where .= ")";

        // print_r($id);exit;
        $arrData = Loader::model('Callplatform')->taskRecordsList($id, $where);
        echo json_encode($arrData);
    }


    /**
     * 添加订单
     */
    public function insertOrder()
    {
        // 病情信息填写了
        if ($_REQUEST['illness_type'] && $_REQUEST['description']) {
            $illness['illness_type'] = $_REQUEST['illness_type'];
            $illness['description'] = $_REQUEST['description'];
            $illness['customer_code'] = $_REQUEST['customer_code'];
            $illness['create_user'] = $_SESSION['user_info']['username'];
            $illness['create_time'] = date('Y-m-d H:i:s');
            // print_r($illness);
            Loader::model('Customer')->insertIllness($illness);
        }
        Loader::model('Ordermanager')->insertOrder($_REQUEST);
    }


    //获取客户地址
    public function synchroAddress()
    {
        $customer_code = $_REQUEST["customer_code"];
        $id = $_REQUEST["id"];
        $arrData = Loader::model('Callplatform')->getAddress($customer_code, $id);
        echo json_encode($arrData);
    }

}